/* ---------------------------- creators/inline.js -------------------------- */
(function() {

   VED.inline = function( element, instanceConfig ) {
      if ( !VED.env.isCompatible )
         return null;

      element = VED.dom.element.get( element );

      // Avoid multiple inline editor instances on the same element.
      if ( element.getEditor() )
         throw 'The editor instance "' + element.getEditor().name + '" is already attached to the provided element.';

      var editor = new VED.editor( instanceConfig, element, VED.ELEMENT_MODE_INLINE ),
         textarea = element.is( 'textarea' ) ? element : null;

      if ( textarea ) {
         editor.setData( textarea.getValue(), null, true );

         //Change element from textarea to div
         element = VED.dom.element.createFromHtml(
            '<div contenteditable="' + !!editor.readOnly + '" class="ved_textarea_inline">' +
               textarea.getValue() +
            '</div>',
            VED.document );

         element.insertAfter( textarea );
         textarea.hide();

         // Attaching the concrete form.
         if ( textarea.$.form )
            editor._attachToForm();
      } else {
         // Initial editor data is simply loaded from the page element content to make
         // data retrieval possible immediately after the editor creation.
         editor.setData( element.getHtml(), null, true );
      }

      // Once the editor is loaded, start the UI.
      editor.on( 'loaded', function() {
         editor.fire( 'uiReady' );

         // Enable editing on the element.
         editor.editable( element );

         // Editable itself is the outermost element.
         editor.container = element;

         // Load and process editor data.
         editor.setData( editor.getData( 1 ) );

         // Clean on startup.
         editor.resetDirty();

         editor.fire( 'contentDom' );

         // Inline editing defaults to "wysiwyg" mode, so plugins don't
         // need to make special handling for this "mode-less" environment.
         editor.mode = 'wysiwyg';
         editor.fire( 'mode' );

         // The editor is completely loaded for interaction.
         editor.status = 'ready';
         editor.fireOnce( 'instanceReady' );
         VED.fire( 'instanceReady', null, editor );

         // give priority to plugins that relay on editor#loaded for bootstrapping.
      }, null, null, 10000 );

      // Handle editor destroying.
      editor.on( 'destroy', function() {
         // Remove container from DOM if inline-textarea editor.
         // Show <textarea> back again.
         if ( textarea ) {
            editor.container.clearCustomData();
            editor.container.remove();
            textarea.show();
         }

         editor.element.clearCustomData();

         delete editor.element;
      });

      return editor;
   };


   VED.inlineAll = function() {
      var el, data;

      for ( var name in VED.dtd.$editable ) {
         var elements = VED.document.getElementsByTag( name );

         for ( var i = 0, len = elements.count(); i < len; i++ ) {
            el = elements.getItem( i );

            if ( el.getAttribute( 'contenteditable' ) == 'true' ) {
               // Fire the "inline" event, making it possible to customize
               // the instance settings and eventually cancel the creation.

               data = {
                  element: el,
                  config: {}
               };

               if ( VED.fire( 'inline', data ) !== false )
                  VED.inline( el, data.config );
            }
         }
      }
   };

   window.onDomReady( function() {
      !VED.disableAutoInline && VED.inlineAll();
   });
})();





